#!/usr/bin/env python
# simple GUI menu to choose the game video mode
 
# import required modules, set directory structure, and initialize pygame
from Tkinter import *
import os
import sys
import cPickle

root = Tk()
root.title("Main menu")
root.geometry("500x360")
 
class OptionsMenu(Frame):
    """ A simple GUI menu to set options for the game."""
    def __init__(self, master = None):
        # Initialize the window
        Frame.__init__(self, master)
        self.grid()
        self.create_widgets()
        
    def create_widgets(self):
        self.vid_mode_chosen = IntVar()
        self.sound = IntVar()
        # the widgets
        # Label for video option
        Label(self, text = "Click one of the video modes below:").grid(row = 0, column = 0, sticky = W)
            
        # Video option one: 800x600 windowed
        Radiobutton(self, text = "800x600 windowed", variable = self.vid_mode_chosen, value = 0).grid(row = 1, column = 0, sticky = W)
        
        # Video option two: 1024x768 windowed
        Radiobutton(self, text = "1024x768 windowed", variable = self.vid_mode_chosen, value = 1).grid(row = 2, column = 0, sticky = W)
        
        # Video option three: 800x600 fullscreen
        Radiobutton(self, text = "800x600 fullscreen", variable = self.vid_mode_chosen, value = 2).grid(row = 3, column = 0, sticky = W)
        
        # Video option four: 1024x768 fullscreen
        Radiobutton(self, text = "1024x768 fullscreen", variable = self.vid_mode_chosen, value = 3).grid(row = 4, column = 0, sticky = W)
        
        # Label for sound option
        Label(self, text = "Do you want sound?").grid(row = 6, column = 0, sticky = W)
        
        # Option sound on (default)
        Radiobutton(self, text = "make noise", variable = self.sound, value = 0).grid(row = 7, column = 0, sticky = W)
        
        # Option sound off
        Radiobutton(self, text = "shut up", variable = self.sound, value = 1).grid(row = 8, column = 0, sticky = W)
        
        # Play button
        Button(self, text = "Play", command = self.launch_game).grid(row = 8, column = 2, sticky = E)
        
        # Quit button
        Button(self, text = "Quit", command = self.exit).grid(row = 8, column = 3, sticky = E)
        
              
    def exit(self):
        exit()
    
    def launch_game(self):
        settings_file = open("data/settings", "w")
        if self.vid_mode_chosen.get() == 0:
            screen_WIDTH = 800
            screen_HEIGHT = 600
            fullscreen = False
        elif self.vid_mode_chosen.get() == 1:
            screen_WIDTH = 1024
            screen_HEIGHT = 768
            fullscreen = False
        elif self.vid_mode_chosen.get() == 2:
            screen_WIDTH = 800
            screen_HEIGHT = 600
            fullscreen = True
        elif self.vid_mode_chosen.get() == 3:
            screen_WIDTH = 1024
            screen_HEIGHT = 768
            fullscreen = True
        
        # Write the user-chosen settings to a file and launch the game
        cPickle.dump(screen_WIDTH, settings_file)
        cPickle.dump(screen_HEIGHT, settings_file)
        cPickle.dump(fullscreen, settings_file)
        if self.sound.get() == 1:
            sound = False
        elif self.sound.get() == 0:
            sound = True
        cPickle.dump(sound, settings_file)
        base_dir = os.getcwd()
        cPickle.dump(base_dir, settings_file)
        settings_file.close()
        os.system("bin/game.py")
        exit()
            
# Starting the window with all the elements defined above
app = OptionsMenu(root)
root.mainloop()
